import api from '@/api';

export default {
  namespace: 'gatherCenter',
  state: {
    publicList: {
      list: [],
      pagination: {},
    },
    billedBills: {
      list: [],
      pagination: {},
    },
    unbilledBills: {
      list: [],
      pagination: {},
    },
    billOrders: {
      list: [],
      pagination: {},
    },
  },
  effects: {
    *fetchPublic({ payload }, { call, put }) {
      const response = yield call(api.hui.gatherCenter.queryPublic, payload);
      const { errorCode, data: list, pagination } = response
      if (errorCode === 0) {
        yield put({
          type: 'savePublicSource',
          payload: {
            list,
            pagination,
          },
        });
      }
      return response
    },
    *fetchBilledBills({ payload }, { call, put }) {
      const response = yield call(api.hui.gatherCenter.queryBills, payload);
      const { errorCode, data: list, pagination } = response
      if (errorCode === 0) {
        yield put({
          type: 'saveBilledBills',
          payload: {
            list,
            pagination,
          },
        });
      }
      return response
    },
    *fetchUnBilledBills({ payload }, { call, put }) {
      const response = yield call(api.hui.gatherCenter.queryUnBills, payload);
      const { errorCode, data: list, pagination } = response
      if (errorCode === 0) {
        yield put({
          type: 'saveUnBilledBills',
          payload: {
            list,
            pagination,
          },
        });
      }
      return response
    },

    *fetchBillOrders({ payload }, { call, put }) {
      const response = yield call(api.hui.gatherCenter.queryBillOrders, payload);
      const { errorCode, data: list, pagination } = response
      if (errorCode === 0) {
        yield put({
          type: 'saveBillOrders',
          payload: {
            list,
            pagination,
          },
        });
      }
      return response
    },
    *receive({ payload }, { call }) {
      return yield call(api.hui.gatherCenter.receive, payload);
    },

  },
  reducers: {
    savePublicSource(state, { payload }) {
      return {
        ...state,
        publicList: payload,
      };
    },
    saveBilledBills(state, { payload }) {
      return {
        ...state,
        billedBills: payload,
      };
    },
    saveUnBilledBills(state, { payload }) {
      return {
        ...state,
        unbilledBills: payload,
      };
    },
    saveBillOrders(state, { payload }) {
      return {
        ...state,
        billOrders: payload,
      };
    },
  },

};
